﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchBug : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetTextBoxHints(searchtxt, "Type your search term here and click search");
            if (Request.QueryString["q"] != null)
                searchtxt.Text = Request.QueryString["q"];

            ltrSearchAdvanced.Text = "hide";

            foreach (var item in EnumUtility.EnumToList<BugManagement.Type>())
            {
                DDLTypeOfBug.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            foreach (var item in EnumUtility.EnumToList<BugManagement.Status>())
            {
                ddlStatus.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            foreach (var item in EnumUtility.EnumToList<BugManagement.Severity>())
            {
                dllSeverity.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            foreach (var item in EnumUtility.EnumToList<BugManagement.Priority>())
            {
                dllPriority.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            DDLTypeOfBug.Items.Add("All");
            ddlStatus.Items.Add("All");
            dllSeverity.Items.Add("All");
            dllPriority.Items.Add("All");
            dllrecentBug.Items.Add("All");

            DDLTypeOfBug.SelectedValue = "All";
            ddlStatus.SelectedValue = "All";
            dllSeverity.SelectedValue = "All";
            dllPriority.SelectedValue = "All";
            dllrecentBug.SelectedValue = "All";
        }
    }

    protected void rdUser_CheckedChanged(object sender, EventArgs e)
    {
        Response.Redirect("/SearchUser.aspx?q=" + HttpUtility.HtmlEncode(searchtxt.Text));
    }

    protected void lbtnAdvancedSearch_Click(object sender, EventArgs e)
    {
        if (lbtnAdvancedSearch.Text == "Advanced Search")
        {
            ltrSearchAdvanced.Text = "";
            lbtnAdvancedSearch.Text = "Simple Search";
        }
        else
        {
            ltrSearchAdvanced.Text = "hide";
            lbtnAdvancedSearch.Text = "Advanced Search";

            // reset default options
            DDLTypeOfBug.SelectedValue = "All";
            ddlStatus.SelectedValue = "All";
            dllSeverity.SelectedValue = "All";
            dllPriority.SelectedValue = "All";
            dllrecentBug.SelectedValue = "All";
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string searchKeyword = searchtxt.Text.Trim();
        var query = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs
                    where item.Description.Contains(searchKeyword) || item.Summary.Contains(searchKeyword)
                    select item;
        if (DDLTypeOfBug.SelectedValue != "All")
            query = query.Where(i => i.Type == DDLTypeOfBug.SelectedIndex);
        if (ddlStatus.SelectedValue != "All")
            query = query.Where(i => i.Status == ddlStatus.SelectedIndex);
        if (dllSeverity.SelectedValue != "All")
            query = query.Where(i => i.Severity == dllSeverity.SelectedIndex);
        if (dllPriority.SelectedValue != "All")
            query = query.Where(i => i.Priority == dllPriority.SelectedIndex);
        if (dllrecentBug.SelectedValue != "All")
            query = query.Where(i => (DateTime.Now - (DateTime)i.LastUpdatedDate).Days < int.Parse(dllrecentBug.SelectedValue));

        // bind query result to list view
        DataList1.DataSource = query.OrderBy(i => i.LastUpdatedDate);
        DataList1.DataBind();
    }

    private void SetTextBoxHints(TextBox textBox, string defaultText)
    {
        textBox.Attributes.Add("onfocus", "clearText(this,'" + defaultText + "')");
        textBox.Attributes.Add("onblur", "resetText(this,'" + defaultText + "')");
        if (textBox.Text == "")
        {
            textBox.Text = defaultText;
            textBox.ForeColor = System.Drawing.Color.Silver;
        }
        else if (textBox.Text == defaultText)
        {
            textBox.ForeColor = System.Drawing.Color.Silver;
        }
        else
        {
            textBox.ForeColor = System.Drawing.Color.Black;
        }
    }
}